package com.xuduo.amd.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    private static String formatter = "yyyy-MM-dd HH:mm:ss";
    private static String YMD_PATTERN = "yyyyMMdd";
    private static String ISOformatter = " yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    static SimpleDateFormat format = new SimpleDateFormat(formatter);
    static SimpleDateFormat ISOformat = new SimpleDateFormat(ISOformatter);
    static SimpleDateFormat YMDformat = new SimpleDateFormat(YMD_PATTERN);


    /**
     * 获取指定格式时间
     *
     * @param date
     * @return
     */
    public static String getCurrentTime(Date date) {
        return format.format(date);
    }

    /**
     * 获取指定格式时间
     *
     * @param date
     * @return
     */
    public static String getYmdPattern(Date date) {
        return format.format(date);
    }

    /**
     * 获取ISO时间
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date dateToISODate(Date dateStr) throws ParseException {
        Date parse = null;
        parse = ISOformat.parse(ISOformat.format(dateStr));
        return parse;

    }


    /**
     * 字符串装成指定格式时间
     *
     * @param StrDate
     * @return
     * @throws ParseException
     */
    public static Date dateToDate(Date StrDate) throws ParseException {
        Date strtodate = null;
        strtodate = ISOformat.parse(ISOformat.format(StrDate));
        return strtodate;

    }

    /**
     * 获取昨天零时
     *
     * @return
     * @throws ParseException
     */
    public static Date getYesterday() throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        String yesterday = format.format(time).substring(0, 10) + "00:00:00";
        return strToDate(yesterday);
    }

    /**
     * 获取当天零时
     *
     * @return
     * @throws ParseException
     */
    public static Date getToday() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String today = format.format(calendar.getTime());
        return strToDate(today);
    }

    /**
     * 获取昨天零时
     *
     * @return
     * @throws ParseException
     */
    public static String getYesterdayString() throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        String yesterday = format.format(time).substring(0, 10) + "00:00:00";
        return yesterday;

    }

    /**
     * 获取当天零时
     *
     * @return
     * @throws ParseException
     */
    public static String getTodayString() throws ParseException {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String today = format.format(calendar.getTime());
        return today;
    }

    public static List<Date> getIntervalTimeList(int interval) throws ParseException {
        Date start = getYesterday();
        Date end = getToday();
        List<Date> list = new ArrayList<>();
        while (start.getTime() <= end.getTime()) {
            list.add(start);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(start);
            calendar.add(Calendar.HOUR, interval);
            if (calendar.getTime().getTime() > end.getTime()) {
                if (!start.equals(end)) {
                    list.add(end);
                }
                start = calendar.getTime();
            } else {
                start = calendar.getTime();

            }
        }
        return list;

    }


}
